{extend name="public/base"/}
{block name="style"}
<style>
    .layui-table th{text-align:center;}
    .layui-table td{text-align:center;}
</style>
{/block}
{block name="content"}
<form class="layui-form" action="{:url('save')}" method="post" lay-filter="settings-form" id="settings-form">
    {:token()}
    {if isset($settings)}
    <input type="hidden" name="settings.setting_id" value="{$settings.setting_id}">
    {/if}
    <div class="layui-form-item">
        <div class="layui-col-xs3">
            <label class="layui-form-label xrequired">渠道</label>
            <div class="layui-input-block">
                <select name="settings.channel_id" lay-search>
                    <option value="">请选择</option>
                    {foreach $channel as $c}
                    <option {if isset($settings) && $c.channel_id==$settings.channel_id}selected{/if} value="{$c.channel_id}">{$c.channel_name}</option>
                    {/foreach}
                </select>
            </div>
        </div>
        <div class="layui-col-xs3">
            <label class="layui-form-label xrequired">可走国家</label>
            <div class="layui-input-block">
                <select name="settings.country_code" lay-search>
                    <option value="">请选择</option>
                    {foreach $counties as $c}
                    <option {if isset($settings) && $c.code_two==$settings.country_code}selected{/if} value="{$c.code_two}">{$c.name_ch}</option>
                    {/foreach}
                </select>
            </div>
        </div>
        <div class="layui-col-xs3">
            <label class="layui-form-label">承诺天数</label>
            <div class="layui-input-block">
                <input type="text" autocomplete="off" placeholder="承诺天数，例：7-15" name="settings.promise_day" {if isset($settings)}value="{$settings.promise_day}"{/if} class="layui-input" />
            </div>
        </div>
        <div class="layui-col-xs3">
            <label class="layui-form-label">超重费</label>
            <div class="layui-input-block">
                <input type="text" autocomplete="off" placeholder="超重费" name="settings.over_weight" {if isset($settings)}value="{$settings.over_weight|abs}"{/if} class="layui-input" />
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-col-xs3">
            <label class="layui-form-label">超长费</label>
            <div class="layui-input-block">
                <input type="text" autocomplete="off" placeholder="超长费" name="settings.over_length" {if isset($settings)}value="{$settings.over_length|abs}"{/if} class="layui-input" />
            </div>
        </div>
        <div class="layui-col-xs6">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input type="text" autocomplete="off" placeholder="备注" name="settings.remark" {if isset($settings)}value="{$settings.remark}"{/if}class="layui-input"/>
            </div>
        </div>
        <div class="layui-col-xs3">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="checkbox" name="settings.status" value="1" {if !isset($settings) || (isset($settings) && $settings.status)}checked{/if} lay-skin="switch">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-col-xs12">
            <label class="layui-form-label">不可走属性</label>
            <div class="layui-input-block">
                <div id="exclude-attr"></div>
                <span id="default-attr" class="layui-hide">{$attrs|json_encode}</span>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">不可走邮编</label>
        <div class="layui-input-block">
            <textarea autocomplete="off" placeholder="不可走邮编，多个使用逗号分开" name="settings.exclude_post_code"  class="layui-textarea">{if isset($settings)}{$settings.exclude_post_code}{/if}</textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <h1>报价单</h1>
        <table class="layui-table">
            <thead>
            <tr>
                <th>开始重量(g)</th>
                <th>结束重量(g)</th>
                <th>续重(g)</th>
                <th>最长边(mm)</th>
                <th>价格</th>
                <th>挂号费</th>
                <th>续重费</th>
                <th>操作费</th>
                <th>燃油费</th>
                <th>清关费</th>
                <th>尾程派送费</th>
                <th>税费</th>
                <th>附加费</th>
                <th>折扣</th>
                <th>体积抛比</th>
                <th>状态</th>
                <th>备注</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            {if isset($settings)}
            {foreach $settings->quotation as $quotation}
            <tr class="data-quotation-row">
                <td><input type="number" value="{$quotation.start_weight|abs}" name="quotation.start_weight[]" class="layui-input" /></td>
                <td><input type="number" value="{$quotation.end_weight|abs}" name="quotation.end_weight[]" class="layui-input" /></td>
                <td><input type="number" value="{$quotation.continue_weight|abs}" name="quotation.continue_weight[]" class="layui-input" /></td>
                <td><input type="number" value="{$quotation.max_length|abs}" name="quotation.max_length[]" class="layui-input" /></td>
                <td><input type="number" value="{$quotation.price|abs}" name="quotation.price[]" class="layui-input" /></td>
                <td><input type="number" value="{$quotation.register_price|abs}" name="quotation.register_price[]" class="layui-input" /></td>
                <td><input type="number" value="{$quotation.continue_weight_price|abs}" name="quotation.continue_weight_price[]" class="layui-input" /></td>
                <td><input type="number" value="{$quotation.operation_price|abs}" name="quotation.operation_price[]" class="layui-input" /></td>
                <td><input type="number" value="{$quotation.fuel_price|abs}" name="quotation.fuel_price[]" class="layui-input" /></td>
                <td><input type="number" value="{$quotation.customs_price|abs}" name="quotation.customs_price[]" class="layui-input" /></td>
                <td><input type="number" value="{$quotation.backend_price|abs}" name="quotation.backend_price[]" class="layui-input" /></td>
                <td><input type="number" value="{$quotation.tax_price|abs}" name="quotation.tax_price[]" class="layui-input" /></td>
                <td><input type="number" value="{$quotation.other_price|abs}" name="quotation.other_price[]" class="layui-input" /></td>
                <td><input type="number" value="{$quotation.discount|abs}" name="quotation.discount[]" class="layui-input" /></td>
                <td><input type="number" value="{$quotation.paobi|abs}" name="quotation.paobi[]" class="layui-input" /></td>
                <td><input type="checkbox" name="quotation.status[]" value="1" {if $quotation.status}checked{/if} lay-skin="switch"></td>
                <td><input type="text" placeholder="备注" value="{$quotation.remark}" name="quotation.remark[]" class="layui-input"/></td>
                <td>
                    <button type="button" class="layui-btn layui-btn-normal layui-btn-xs data-quotation-add" style="margin-bottom: 3px;">
                        <i class="layui-icon layui-icon-add-1"></i>
                    </button>
                    <br/>
                    <button type="button" class="layui-btn layui-btn-danger layui-btn-xs data-quotation-remove">
                        <i class="layui-icon layui-icon-delete"></i>
                    </button>
                </td>
                <input type="hidden" name="quotation.quotation_id[]" value="{$quotation.quotation_id}">
            </tr>
            {/foreach}
            {/if}
            </tbody>
        </table>
    </div>
    <div class="layui-form-item">
        <div class="layui-col-xs12 text-center">
            <button type="reset" class="layui-btn">重置</button>
            <button type="submit" class="layui-btn layui-btn-normal" lay-submit lay-filter="settings-form">保存</button>
        </div>
    </div>
</form>
<script type="text/html" id="data-quotation-template">
    <tr class="data-quotation-row">
        <td><input type="number" autocomplete="off" placeholder="开始重量" name="quotation.start_weight[]" class="layui-input" /></td>
        <td><input type="number" autocomplete="off" placeholder="结束重量" name="quotation.end_weight[]" class="layui-input" /></td>
        <td><input type="number" autocomplete="off" placeholder="续重" name="quotation.continue_weight[]" class="layui-input" /></td>
        <td><input type="number" autocomplete="off" placeholder="最长边" name="quotation.max_length[]" class="layui-input" /></td>
        <td><input type="number" autocomplete="off" placeholder="价格" name="quotation.price[]" class="layui-input" /></td>
        <td><input type="number" autocomplete="off" placeholder="挂号费" name="quotation.register_price[]" class="layui-input" /></td>
        <td><input type="number" autocomplete="off" placeholder="续重费" name="quotation.continue_weight_price[]" class="layui-input" /></td>
        <td><input type="number" autocomplete="off" placeholder="操作费" name="quotation.operation_price[]" class="layui-input" /></td>
        <td><input type="number" autocomplete="off" placeholder="燃油费" name="quotation.fuel_price[]" class="layui-input" /></td>
        <td><input type="number" autocomplete="off" placeholder="清关费" name="quotation.customs_price[]" class="layui-input" /></td>
        <td><input type="number" autocomplete="off" placeholder="尾程派送费" name="quotation.backend_price[]" class="layui-input" /></td>
        <td><input type="number" autocomplete="off" placeholder="税费" name="quotation.tax_price[]" class="layui-input" /></td>
        <td><input type="number" autocomplete="off" placeholder="附加费" name="quotation.other_price[]" class="layui-input" /></td>
        <td><input type="number" autocomplete="off" placeholder="折扣，默认100" name="quotation.discount[]" class="layui-input" /></td>
        <td><input type="number" autocomplete="off" placeholder="体积抛比" name="quotation.paobi[]" class="layui-input" /></td>
        <td><input type="checkbox" name="quotation.status[]" value="1" checked lay-skin="switch"></td>
        <td><input type="text" autocomplete="off" placeholder="备注" name="quotation.remark[]" class="layui-input"/></td>
        <td>
            <button type="button" class="layui-btn layui-btn-normal layui-btn-xs data-quotation-add" style="margin-bottom: 3px;">
                <i class="layui-icon layui-icon-add-1"></i>
            </button>
            <br/>
            <button type="button" class="layui-btn layui-btn-danger layui-btn-xs data-quotation-remove">
                <i class="layui-icon layui-icon-delete"></i>
            </button>
        </td>
    </tr>
</script>
{/block}
{block name="script"}
<script type="text/javascript" src="__PUBLIC__/js/jquery.serializejson.min.js"></script>
<script type="text/javascript">
    //一般直接写在一个js文件中
    layui.extend({
        xmSelect: 'xm-select'
    }).use(['layer', 'form', 'jquery', 'longli', 'xmSelect'], function()
    {
        var layer = layui.layer
            , form = layui.form
            , $ = layui.jquery;

        // 自动添加报价单详情
        if($(".layui-table .data-quotation-row").length < 1)
        {
            var html = $("#data-quotation-template").html();
            $(".layui-table tbody").prepend(html);
            form.render();
        }
        // 添加行
        $(".layui-table").on("click", ".data-quotation-add", function()
        {
            var html = $("#data-quotation-template").html();
            $(".layui-table .data-quotation-row:last").after(html);
            form.render();
        });

        // 删除行
        $(".layui-table").on("click", ".data-quotation-remove",function()
        {
            if($(".layui-table .data-quotation-row").length == 1)
            {
                layer.msg('只有一条不允许删除。', {time : 2000});
                return;
            }
            $(this).parent().parent().remove();
        });

        // 提交表单
        form.on("submit(settings-form)", function (data)
        {
            var json = $("#settings-form").serializeJSON();
            layui.longli.postJson('{:url("save")}', json, function(response)
            {
                var icon = response.code == 1 ? 6 : 5;
                layer.msg(response.msg, {icon: icon, time: 1500}, function()
                {
                    if(response.code == 1)
                    {
                        location.href = "{:url('index')}";
                    }
                });
            });
            return false;
        });

        renderSearch($("#exclude-attr").get(0), JSON.parse($("#default-attr").text()));
        /**
         * 渲染多选下拉框
         * @param node 需要渲染的节点
         * @param defVal 默认值
         */
        function renderSearch(node, defVal)
        {
            var xargs = {
                el: node,
                prop: {
                    name: 'attr_name',
                    value: 'attr_id',
                },
                data: [],
                name: 'settings.exclude_attr',
                filterable: true,
                remoteSearch: true,
                on: function(data)
                {

                },
                remoteMethod: function (text, callback, show)
                {
                    text = text.trim();
                    $.post("{:url('cha/attrs/get')}", {code: text}).done(function (response)
                    {
                        if(response.code != 1) return callback([]);
                        callback(response.data);
                    });
                }
            };
            if(defVal)
            {
                for(let i in defVal)
                {
                    xargs.data.push({attr_id: defVal[i].attr_id, attr_name: defVal[i].attr_name, selected: true});
                }
            }
            layui.xmSelect.render(xargs);
        }
    });
</script>
{/block}